﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using navigation.Data;

namespace navigation.Controls
{
    /// <summary>
    /// Interaction logic for BurnDownChart.xaml
    /// </summary>
    public partial class BurnDownChart : UserControl
    {
        public BurnDownChart()
        {
            InitializeComponent();
        }

        private static int timePerStoryPoints = 1;

        public static DependencyProperty ChartItemsProperty = DependencyProperty.Register("ChartItems", typeof(ObservableCollection<UserStory>), typeof(BurnDownChart));

        public ObservableCollection<UserStory> ChartItems
        {
            get
            {
                return (ObservableCollection<UserStory>)GetValue(BurnDownChart.ChartItemsProperty);
            }
            set
            {
                SetValue(BurnDownChart.ChartItemsProperty, value);
            }
        }

        private ObservableCollection<KeyValuePair<int, int>> valueList = new ObservableCollection<KeyValuePair<int, int>>();
        private ObservableCollection<KeyValuePair<int, int>> idealValueList;

        private int wholeTime = 0;
        private int wholeStoryPoints = 0;

        private void BDChart_Loaded(object sender, RoutedEventArgs e)
        {
           
            foreach (UserStory us in ChartItems)
            {
                wholeStoryPoints += us.StoryPoints;
                wholeTime += us.StoryPoints * timePerStoryPoints;
            }

            valueList.Add(new KeyValuePair<int, int>(1, wholeStoryPoints));
            //valueList.Add(new KeyValuePair<int, int>(2, 95));
            //valueList.Add(new KeyValuePair<int, int>(3, 80));
            //valueList.Add(new KeyValuePair<int, int>(4, 60));
            //valueList.Add(new KeyValuePair<int, int>(5, 55));
            //valueList.Add(new KeyValuePair<int, int>(6, 35));
            //valueList.Add(new KeyValuePair<int, int>(7, 20));
            //valueList.Add(new KeyValuePair<int, int>(8, 5));
            //valueList.Add(new KeyValuePair<int, int>(9, 0));

            //idealValueList.Add(new KeyValuePair<int, int>(1, wholeStoryPoints));
            //idealValueList.Add(new KeyValuePair<int, int>(wholeTime, 0));
            idealValueList = GenerateIdealPlot(wholeStoryPoints, wholeTime);

            var chartValue = new ObservableCollection<ObservableCollection<KeyValuePair<int, int>>>();           
            chartValue.Add(idealValueList);
            chartValue.Add(valueList);
            BDChart.DataContext = chartValue;
        }

        private ObservableCollection<KeyValuePair<int, int>> GenerateIdealPlot(int wholeStoryPoints, int wholetime)
        {
            ObservableCollection<KeyValuePair<int, int>> idealValueList = new ObservableCollection<KeyValuePair<int, int>>();

            for (int i = 0; i <= wholetime; i+=4)
            {
                int x = wholeStoryPoints - i*wholeStoryPoints/wholetime ;
                idealValueList.Add(new KeyValuePair<int, int>(i + 1, x));
            }
            return idealValueList;
        }

        private void BDChart_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            ObservableCollection<UserStory> newChartItems = e.NewValue as ObservableCollection<UserStory>;
            Console.WriteLine(newChartItems);
            //foreach (UserStory us in newChartItems)
            //{
            //    wholeStoryPoints += us.StoryPoints;
            //    wholeTime += us.StoryPoints * timePerStoryPoints;
            //}

            //valueList.Add(new KeyValuePair<int, int>(1, wholeStoryPoints));
            //idealValueList = GenerateIdealPlot(wholeStoryPoints, wholeTime);

            //var chartValue = new ObservableCollection<ObservableCollection<KeyValuePair<int, int>>>();            
            //chartValue.Add(idealValueList);
            //chartValue.Add(valueList);
            //BDChart.DataContext = chartValue;
        }
       
    }
}
